package org.chen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.enu.ContentPosition;
import org.chen.exception.ServiceException;
import org.chen.model.MovieJson;
import org.chen.response.BatchProgressResponse;
import org.chen.response.BatchResponse;
import org.chen.response.R;
import org.chen.service.impl.MovieService;
import org.chen.template.MovieDetail;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/movies")
@RequiredArgsConstructor
@Slf4j
public class MovieController {
    
    private final MovieService movieService;

    @GetMapping("/list")
    public R<IPage<MovieJson>> getMovieList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        try {
            Page<MovieJson> page = new Page<>(current, size);
            IPage<MovieJson> moviePage = movieService.getMovieListPage(page, name);
            return R.success(moviePage);
        } catch (Exception e) {
            log.error("获取电影列表失败", e);
            return R.error("获取电影列表失败: " + e.getMessage());
        }
    }

    @PostMapping("/upload")
    public R<BatchResponse> uploadExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam("contentPosition") ContentPosition contentPosition) {
        try {
            String batchNo = movieService.startBatchProcess(file, contentPosition);

            BatchResponse response = new BatchResponse();
            response.setBatchNo(batchNo);
            response.setMessage("文件已接收，正在处理中");

            return R.success(response);
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/batch/{batchNo}/progress")
    public R<BatchProgressResponse> getBatchProgress(@PathVariable String batchNo) {
        BatchProgressResponse progress = movieService.getBatchProgress(batchNo);
        return R.success(progress);
    }

    @GetMapping("/show/{name}")
    public R<MovieDetail> getMovieJson(@PathVariable String name) {
        try {
            MovieDetail movieDetail = movieService.getMovieDetail(name);
            return R.success(movieDetail);
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/{name}")
    public MovieDetail getMovieOriginJson(@PathVariable String name) {
        try {
            return movieService.getMovieDetail(name);
        } catch (ServiceException e) {
            return null;
        }
    }

    @GetMapping("/phim/{name}")
    public MovieDetail getMovieV2OriginJson(@PathVariable String name) {
        try {
            return movieService.getMovieDetail(name);
        } catch (ServiceException e) {
            return null;
        }
    }

    // 删除单个电影
    @DeleteMapping("/{id}")
    public R<Void> deleteMovie(@PathVariable Long id) {
        try {
            movieService.deleteMovieById(id);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    // 批量删除电影
    @DeleteMapping("/batch")
    public R<Void> batchDeleteMovies(@RequestBody List<Long> ids) {
        try {
            movieService.batchDeleteMovies(ids);
            return R.success();
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    // 批量导出选中的电影数据
    @PostMapping("/export")
    public R<List<MovieDetail>> exportMovies(@RequestBody List<Long> ids) {
        try {
            List<MovieDetail> movieDetails = movieService.exportMoviesData(ids);
            return R.success(movieDetails);
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }
}